import React from 'react';
import { connect } from 'dva';
import { FirstHeaderC } from '../../../../comps';
import DetailC from '../../../PortfolioManagement/portfolioDefinition/myComps/detailC';
import CardC from '../../Barra/comps/cardC';
import styles from './index.less';
class PortfolioSetting extends React.Component {
  constructor() {
    super();
    this.state = {
      addPortfolio: false, // 是否新增了子组合标志位
      rootItem: {
        portfolioName: undefined,
        portfolioCode: undefined,
        isInvestment: undefined,
        isExtra: undefined,
        tradeMode: undefined,
      },
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: 'portfolioDefinitionM/queryPortfolioPage',
      payload: {
        currentPage: 1,
        numInPage: 20,
        id: this.props.rootId,
      },
      callback: (data) => {
        console.log(data);
        if (data && data.dataSource && data.dataSource.length) {
          let rootItem = {
            name: data.dataSource[0].portfolioName,
            value: data.dataSource[0].portfolioCode,
            isInvestment: data.dataSource[0].isInvestment,
            isExtra: data.dataSource[0].isExtra,
            tradeMode: data.dataSource[0].tradeMode,
          };
          this.setState({
            rootItem: rootItem,
          });
        }
      },
    });
  }

  // 记录是否新增了子组合，当子组合新增了，关闭组合设置页面时要向框架抛出事件addChildProduct
  addPortfolio = () => {
    this.setState({
      addPortfolio: true,
    });
  };

  //关闭组合设置弹窗
  onClose = (e) => {
    if (this.state.addPortfolio) {
      this.props.addChildProduct();
    } else {
      this.props.changePage(1);
    }
  };

  render() {
    return (
      <div className={styles.BaseInfoForm}>
        <CardC title='组合设置' type='back' showContent={true} titleIsShow={false} changeFormPage={() => this.onClose()} />
        {/* 内容 */}
        <div className={styles.baseInfo}>
          <DetailC type={'workStation'} onClose={() => this.onClose()} addPortfolio={() => this.addPortfolio()} rootId={this.props.rootId} rootItem={this.state.rootItem} />
        </div>
      </div>
    );
  }
}
export default connect(({ portfolioDefinitionM }) => ({ portfolioDefinitionM }))(PortfolioSetting);
